﻿using Microsoft.AspNetCore.Mvc;
using TodoApi.Models;

namespace TodoApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FileUploadController
    {
        private readonly IWebHostEnvironment _env;
        private readonly StudentContext _context;
        public FileUploadController(IWebHostEnvironment env,StudentContext context)
        {
            _env = env;
            _context = context;
        }
        [HttpPost]  
        public  async Task<List<FilePath>> UploadAsync(List<IFormFile> files)
        {
            var fileList = new List<FilePath>();
            var dateName = DateTime.Now;
            string rootRoot = _env.ContentRootPath + @"\wwwroot\UploadFiles\" + dateName.ToString("yyyyMMdd") + @"\";
            //查看是否存在当天日期的文件夹
            if (!Directory.Exists(rootRoot))
            {
                Directory.CreateDirectory(rootRoot);
            }
            foreach (var item in files)
            {   //后缀
                var fileSuffix = Path.GetExtension(item.FileName);
                //时间结尾的文件名
                var filePath = item.FileName.Substring(0, item.FileName.LastIndexOf('.'))+"_" + dateName.ToString("HHmmss") + fileSuffix;
                using (var systeam = System.IO.File.Create(rootRoot + filePath))
                {
                    item.CopyTo(systeam);
                }
                var file = new FilePath
                {
                    Id = Guid.NewGuid(),
                    Path = filePath
                };
                //添加到图片表
                await _context.FilePath.AddAsync(file);
                fileList.Add(file);
            }
            await _context.SaveChangesAsync();
            return fileList;
        }
        [HttpPost]
        public async Task<Avatar> AvatarUploadAsync(IFormFile file)
        {
            var dateName = DateTime.Now;
            string rootRoot = _env.ContentRootPath + @"\wwwroot\UploadFiles\" + dateName.ToString("yyyyMMdd") + @"\";
            //查看是否存在当天日期的文件夹
            if (!Directory.Exists(rootRoot))
            {
                Directory.CreateDirectory(rootRoot);
            }
            //后缀
            var fileSuffix = Path.GetExtension(file.FileName);
            //时间结尾的文件名
            var filePath = file.FileName.Substring(0, file.FileName.LastIndexOf('.')) + "_" + dateName.ToString("HHmmss") + fileSuffix;
            using (var systeam = System.IO.File.Create(rootRoot + filePath))
            {
                file.CopyTo(systeam);
            }
            var avatar = new Avatar
            {
                Id = Guid.NewGuid(),
                Path = dateName.ToString("yyyyMMdd") + @"\"+filePath
            };
            //添加到头像表
            await _context.Avatar.AddAsync(avatar);
            await _context.SaveChangesAsync();
            return avatar;
        }
        [HttpGet]
        public  string ReadImageById(string id)
        {
            return _env.ContentRootPath + @"wwwroot\UploadFiles\"+_context.Avatar.Where(x => x.Id == Guid.Parse(id)).Select(x => x.Path).FirstOrDefault();
        }

    }
}
